package com.guowei.web.controller;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/icon")
public class IconController {

    private static byte[] FOLDER_ICON;

    private static Map<String, byte[]> FILE_ICON;

    static {
        FOLDER_ICON = folderIconByteArray();
        FILE_ICON = fileIconByteArrayMap();
    }

    static byte[] folderIconByteArray() {
        InputStream folderIconInputStream = IconController.class.getClassLoader().getResourceAsStream("icon/folder/folder.ico");
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] b = new byte[4096];
        int len;
        try {
            while ((len = folderIconInputStream.read(b)) != -1) {
                byteArrayOutputStream.write(b, 0, len);
            }
            folderIconInputStream.close();
            byteArrayOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return byteArrayOutputStream.toByteArray();
    }

    static Map<String, byte[]> fileIconByteArrayMap() {
        Map<String, byte[]> fileIconByteArrayMap = new HashMap<>();
        try {
            Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:/icon/file/*.ico");
            for (Resource resource : resources) {
                InputStream fileIconInputStream = resource.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] b = new byte[4096];
                int len;
                while ((len = fileIconInputStream.read(b)) != -1) {
                    byteArrayOutputStream.write(b, 0, len);
                }
                fileIconInputStream.close();
                byteArrayOutputStream.close();
                fileIconByteArrayMap.put(resource.getFilename(), byteArrayOutputStream.toByteArray());
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return fileIconByteArrayMap;
    }

    @RequestMapping("/folder")
    public void folder(HttpServletResponse response) throws IOException {
        response.setContentType("image/jpg");
        response.getOutputStream().write(FOLDER_ICON);
        response.getOutputStream().flush();
    }

    @RequestMapping("/file")
    public void file(String fileType, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpg");
        byte[] b;
        if (fileType != null && !fileType.trim().equals("")) {
            b = FILE_ICON.get(fileType.toLowerCase() + ".ico");
            if (b == null) {
                b = FILE_ICON.get("unknown.ico");
            }
        } else {
            b = FILE_ICON.get("unknown.ico");
        }
        response.getOutputStream().write(b);
        response.getOutputStream().flush();
    }
}
